﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;
using System.Data.SQLite;
using MySql.Data.MySqlClient;

namespace Cashier_DAO.db
{
    public class CommandFactory
    {
        /// <summary>
        /// 创建DbCommand
        /// </summary>
        /// <returns></returns>
        public static DbCommand CreateCommand()
        {
            DbCommand cmd = null;
            try
            {
                if (ConnectionFactory.DatabaseType == DatabaseType.SQLite)
                    cmd = new SQLiteCommand();
                else if (ConnectionFactory.DatabaseType == DatabaseType.MySQL)
                    cmd = new MySqlCommand();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return cmd;
        }

        /// <summary>
        /// 创建DbCommand
        /// </summary>
        /// <param name="commandText"></param>
        /// <returns></returns>
        public static DbCommand CreateCommand(String commandText)
        {
            DbCommand cmd = null;
            try
            {
                if (ConnectionFactory.DatabaseType == DatabaseType.SQLite)
                    cmd = new SQLiteCommand(commandText);
                else if (ConnectionFactory.DatabaseType == DatabaseType.MySQL)
                    cmd = new MySqlCommand(commandText);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return cmd;
        }

        /// <summary>
        /// 创建DbCommand
        /// </summary>
        /// <param name="commandText"></param>
        /// <param name="conn"></param>
        /// <returns></returns>
        public static DbCommand CreateCommand(String commandText, DbConnection conn)
        {
            DbCommand cmd = null;
            try
            {
                if (ConnectionFactory.DatabaseType == DatabaseType.SQLite)
                    cmd = new SQLiteCommand(commandText, (SQLiteConnection)conn);
                else if (ConnectionFactory.DatabaseType == DatabaseType.MySQL)
                    cmd = new MySqlCommand(commandText, (MySqlConnection)conn);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return cmd;
        }
    }
}
